home *** CD-ROM | disk | FTP | other *** search
/ LiquidLibrary 2005 February / LiquidLibrary 2005 February - Disc 1.iso / pc / Portfolio Browser / Filters / PDF / LIB / gs_diskf.ps < prev    next >
Text File  |  2003-01-03  |  7KB  |  215 lines

  1. %    Copyright (C) 1996 Aladdin Enterprises.  All rights reserved.
  2. % This software is licensed to a single customer by Artifex Software Inc.
  3. % under the terms of a specific OEM agreement.
  4.  
  5. % $RCSfile$ $Revision$
  6. % Support for converting Type 1 fonts without eexec encryption to
  7. % Type 4 fonts that load individual character outlines on demand.
  8.  
  9. % If DISKFONTS is true, we load individual CharStrings as they are needed.
  10. % (This is intended primarily for machines with very small memories.)
  11. % Initially, the character definition is the file position of the definition;
  12. % this gets replaced with the actual CharString.
  13. % Note that if we are loading characters lazily, CharStrings is writable.
  14.  
  15. % _Cstring must be long enough to hold the longest CharString for
  16. % a character defined using seac.  This is lenIV + 4 * 5 (for the operands
  17. % of sbw, assuming div is not used) + 2 (for sbw) + 3 * 5 (for the operands
  18. % of seac other than the character codes) + 2 * 2 (for the character codes)
  19. % + 2 (for seac), i.e., lenIV + 43.
  20.  
  21. /_Cstring 60 string def
  22.  
  23. % When we initially load the font, we call
  24. %    <index|charname> <length> <readproc> cskip_C
  25. % to skip over each character definition and return the file position instead.
  26. % This substitutes for the procedure
  27. %    <index|charname> <length> string currentfile exch read[hex]string pop
  28. %      [encrypt]
  29. % What we actually store in the CharString is fileposition * 1000 + length,
  30. %   negated if the string is stored in binary form.
  31.  
  32. /cskip_C
  33.  { exch dup 1000 ge 3 index type /nametype ne or
  34.     { % This is a Subrs string, or the string is so long we can't represent
  35.       % its length.  Load it now.
  36.       exch exec
  37.     }
  38.     { % Record the position and length, and skip the string.
  39.       dup currentfile fileposition 1000 mul add
  40.       2 index 3 get /readstring cvx eq { neg } if
  41.       3 1 roll
  42.       dup _Cstring length idiv
  43.        { currentfile _Cstring 3 index 3 get exec pop pop
  44.        } repeat
  45.       _Cstring length mod _Cstring exch 0 exch getinterval
  46.       currentfile exch 3 -1 roll 3 get exec pop pop
  47.     }
  48.    ifelse
  49.  } bind def
  50.  
  51. % Load a CharString from the file.  The font is the top entry
  52. % on the dictionary stack.
  53. /load_C        % <charname> <fileposandlength> load_C -
  54.  { dup abs 1000 idiv FontFile exch setfileposition
  55.    CharStrings 3 1 roll
  56.    .currentglobal CharStrings .gcheck .setglobal exch
  57.    dup 0 lt
  58.     { neg 1000 mod string FontFile exch readstring }
  59.     { 1000 mod string FontFile exch readhexstring }
  60.    ifelse pop
  61.    exch .setglobal
  62. % If the CharStrings aren't encrypted on the file, encrypt now.
  63.    Private /-| get 0 get
  64.    dup type /nametype ne
  65.     { dup length 5 sub 5 exch getinterval exec }
  66.     { pop }
  67.    ifelse dup 4 1 roll put
  68. % If the character is defined with seac, load its components now.
  69.    mark exch seac_C
  70.    counttomark
  71.     { StandardEncoding exch get dup CharStrings exch get
  72.       dup type /integertype eq { load_C } { pop pop } ifelse
  73.     } repeat
  74.    pop        % the mark
  75.  } bind def
  76.  
  77. /seac_C        % <charstring> seac_C <achar> <bchar> ..or nothing..
  78.  { dup length _Cstring length le
  79.     { 4330 exch _Cstring .type1decrypt exch pop
  80.       dup dup length 2 sub 2 getinterval <0c06> eq    % seac
  81.        { dup length
  82.          Private /lenIV known { Private /lenIV get } { 4 } ifelse
  83.      exch 1 index sub getinterval
  84. % Parse the string just enough to extract the seac information.
  85. % We assume that the only possible operators are hsbw, sbw, and seac,
  86. % and that there are no 5-byte numbers.
  87.      mark 0 3 -1 roll
  88.       { exch
  89.          { { dup 32 lt
  90.               { pop 0 }
  91.           { dup 247 lt
  92.              { 139 sub 0 }
  93.              { dup 251 lt
  94.             { 247 sub 256 mul 108 add 1 1 }
  95.             { 251 sub -256 mul -108 add -1 1 }
  96.                ifelse
  97.              }
  98.             ifelse
  99.           }
  100.          ifelse
  101.            }            % 0
  102.            { mul add 0 }        % 1
  103.          }
  104.         exch get exec
  105.       }
  106.      forall pop
  107.      counttomark 1 add 2 roll cleartomark    % pop all but achar bchar
  108.        }
  109.        { pop    % not seac
  110.        }
  111.       ifelse
  112.     }
  113.     { pop    % punt
  114.     }
  115.    ifelse
  116.  } bind def
  117.  
  118. % Define replacement procedures for loading fonts.
  119. % If DISKFONTS is true and the body of the font is not encrypted with eexec:
  120. %    - Prevent the CharStrings from being made read-only.
  121. %    - Substitute a different CharString-reading procedure.
  122. % (eexec disables this because the implicit 'systemdict begin' hides
  123. % the redefinitions that make the scheme work.)
  124. % We assume that:
  125. %    - The magic procedures (-|, -!, |-, and |) are defined with
  126. %    executeonly or readonly;
  127. %    - The contents of the reading procedures are as defined in bdftops.ps;
  128. %    - The font includes the code
  129. %    <font> /CharStrings <CharStrings> readonly put
  130. /.loadfontdict 6 dict def mark
  131.  /begin            % push this dict after systemdict
  132.   { dup begin
  133.     //systemdict eq { //.loadfontdict begin } if
  134.   } bind
  135.  /end            % match begin
  136.   { currentdict end
  137.     //.loadfontdict eq currentdict //systemdict eq and { end } if
  138.   } bind
  139.  /dict            % leave room for FontFile, BuildChar, BuildGlyph
  140.   { 3 add dict
  141.   } bind
  142.  /executeonly        % for reading procedures
  143.   { readonly
  144.   }
  145.  /noaccess        % for Subrs strings and Private dictionary
  146.   { readonly
  147.   }
  148.  /readonly        % for procedures and CharStrings dictionary
  149.   {    % We want to take the following non-standard actions here:
  150.       %   - If the operand is the CharStrings dictionary, do nothing;
  151.     %   - If the operand is a number (a file position replacing the
  152.     %    actual CharString), do nothing;
  153.     %   - If the operand is either of the reading procedures (-| or -!),
  154.     %    substitute a different one.
  155.     dup type /dicttype eq        % CharStrings or Private
  156.     count 2 gt and
  157.      { 1 index /CharStrings ne { readonly } if }
  158.      { dup type /arraytype eq        % procedure or data array
  159.     { dup length 5 ge 1 index xcheck and
  160.        { dup 0 get /string eq
  161.          1 index 1 get /currentfile eq and
  162.          1 index 2 get /exch eq and
  163.          1 index 3 get dup /readstring eq exch /readhexstring eq or and
  164.          1 index 4 get /pop eq and
  165.           { /cskip_C cvx 2 packedarray cvx
  166.           }
  167.           { readonly
  168.           }
  169.          ifelse
  170.        }
  171.        { readonly
  172.        }
  173.       ifelse
  174.     }
  175.     { dup type /stringtype eq    % must be a Subr string
  176.        { readonly }
  177.       if
  178.     }
  179.        ifelse
  180.      }
  181.     ifelse
  182.   } bind
  183.  /definefont        % to insert BuildChar/Glyph and change FontType
  184.   { dup /FontType get 1 eq
  185.      { dup /FontType 4 put
  186.        dup /BuildChar /build_C load put
  187.        dup /BuildGlyph /build_C load put
  188.      }
  189.     if definefont
  190.   } bind
  191. counttomark 2 idiv { .loadfontdict 3 1 roll put } repeat pop
  192. .loadfontdict readonly pop
  193.  
  194. % Define the BuildChar and BuildGlyph procedures for modified fonts.
  195. % A single procedure serves for both.
  196. /build_C        % <font> <code|name> build_C -
  197.  { 1 index begin
  198.    dup dup type /integertype eq { Encoding exch get } if
  199.         % Stack: font code|name name
  200.    dup CharStrings exch .knownget not
  201.     { 2 copy eq { exch pop /.notdef exch } if
  202.       QUIET not
  203.        { (Substituting .notdef for ) print = flush }
  204.        { pop }
  205.       ifelse
  206.       /.notdef CharStrings /.notdef get
  207.     } if
  208.         % Stack: font code|name name charstring   
  209.    dup type /integertype eq
  210.     { load_C end build_C }
  211.     { end .type1execchar }
  212.    ifelse
  213.  } bind def
  214.